package com.zzw.testdemo01.handler;

import com.zzw.testdemo01.IdUtils;
import net.minecraft.entity.merchant.villager.VillagerEntity;
import net.minecraft.entity.merchant.villager.VillagerProfession;
import net.minecraft.entity.projectile.ProjectileEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.item.MerchantOffer;
import net.minecraft.item.MerchantOffers;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.ProjectileImpactEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.List;
import java.util.stream.Collectors;

@Mod.EventBusSubscriber(modid = IdUtils.MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE)
public class VillagerEventHandler {

    // 注册事件监听器
    public VillagerEventHandler() {
        MinecraftForge.EVENT_BUS.register(this);
    }


//    @SubscribeEvent
    public static void onPlayerInteract(PlayerInteractEvent.EntityInteract event) {
        // 检查玩家是否正在与村民交互
        if (event.getTarget() instanceof VillagerEntity) {
            VillagerEntity villager = (VillagerEntity) event.getTarget();
            // 获取村民的职业等级
            int level = villager.getVillagerData().getLevel();
            // 动态修改村民的交易列表
            modifyVillagerTrades(villager, level);
        }
    }

    private static void modifyVillagerTrades(VillagerEntity villager, int level) {

        MerchantOffers offers = villager.getOffers();
        MerchantOffers newOffers = new MerchantOffers();
        offers.forEach(offer -> {

            ItemStack costA = offer.getCostA();
            ItemStack costB = offer.getCostB();
            int maxUses = offer.getMaxUses();
            if (costB.isEmpty()) {
                ItemStack result = offer.getResult();
                MerchantOffer newTrade = new MerchantOffer(
                        costA, // 购买物品
                        result, // 出售物品
                        maxUses, // 最大交易次数
                        offer.getXp(), // 经验值增加
//                        offer.getPriceMultiplier() // 价格折扣
                        0.8F // 价格折扣
                );

                newOffers.add(newTrade);
            }else{
                newOffers.add(offer);
            }
        });

        // 添加新的交易
        villager.setOffers(newOffers);
    }




}
